--UnitDestroyed() --> "unitID, unitDefID, teamID, attackerID?" 
--UnitCreated() --> "unitID, unitDefID, teamID, builderID"

function gadget:GetInfo()
   return {
      name      = "Killcount Tracker and Respawner",
      desc      = "tracks the total killcount of each unit respawns deceased units",
      author    = "Peeps",
      date      = "w/e",
      license   = "to kill",
      layer     = 0,
      enabled   = true,
   }
end


if (not gadgetHandler:IsSyncedCode()) then
   return false
end

local GetUnitExperience = Spring.GetUnitExperience

local unitTable = {}
local winThreshold = 2

function gadget:UnitDamaged(_, _, teamID, _, _, _, attackerID)
	if GG['donePicking'] == true then
		if attackerID ~= nil then
			attackerExp = GetUnitExperience(attackerID)
			if attackerExp > winThreshold then
				local winningTeam = Spring.GetUnitTeam(attackerID)
				if winningTeam == GG['kills'][1] or winningTeam == GG['kills'][2] then
					GG.gameOver(winningTeam, teamID)
					gadgetHandler:RemoveGadget()
				end
			end
		end	
	end
end
	
	

function gadget:GameFrame(t)
	if (t % (1*30) < 0.1) then 
		if GG['kills'][1] == -1 and GG['kills'][2] == -1 and GG['donePicking'] == true then
			--Spring.Echo("removing Kills gadget")
			gadgetHandler:RemoveGadget()
		end
	end
end

	